package com.sphd.emanu.activity.msg;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.UserMessage;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Author Hu
 * @Time 2017/7/27 -- 10:51.
 * @Email 87683202@qq.com
 */
public class MsgActivity extends MVPBaseActivity<MsgContract.View, MsgPresenter> implements MsgContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.msg_time)
    TextView msgTime;
    @BindView(R.id.msg_content)
    TextView msgContent;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.btn_cancle)
    Button btnCancle;
    Intent intent;
    Bundle bundle;
    UserMessage userMessage;
    @BindView(R.id.layout_btn)
    LinearLayout layoutBtn;
    List<Moudle> moudles;
    boolean isCanDo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        userMessage = (UserMessage) bundle.getSerializable(Constant.USER_MSG);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_DOSOMETHING_10);
        if (!isCanDo){
            btnConfirm.setVisibility(View.GONE);
            btnCancle.setVisibility(View.GONE);
        }
        setTitleBar(toolbar, userMessage.getState() == 1 ? "未处理" : userMessage.getState() == 2 ? "已接收" : "已忽略");
        layoutBtn.setVisibility(userMessage.getState() == 1 ? View.VISIBLE : View.GONE);
        userName.setText(userMessage.getSenderName());
        msgTime.setText(TimeUtils.dateToString(userMessage.getSendTime(), TimeUtils.YEAR_MONTH_DAY_HOUR_MIN));
        msgContent.setText(userMessage.getContent());
    }

    @OnClick({R.id.btn_confirm, R.id.btn_cancle})
    public void onViewClicked(View view) {
        showProgressDialog();
        switch (view.getId()) {
            case R.id.btn_confirm:
                mPresenter.updateMessage(userMessage.getId(),2);
                break;
            case R.id.btn_cancle:
                mPresenter.updateMessage(userMessage.getId(),2);
                break;
        }
    }

    @Override
    public void setRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast(getString(R.string.up_success));
            setResult(Constant.REQUEST_CODE);
            finish();
        } else {
            toast(getString(R.string.up_failure));
        }
    }
}
